package com.sneakxy.utils;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class StringUtilsExt {

	public static String replace(String str, Map<String, Object> params) {
		StringBuffer result = new StringBuffer();
		Pattern pattern = Pattern.compile("\\[([^\\[\\]]+)\\]");
		Matcher matcher = pattern.matcher(str);
		String key = null;
		Object value = null;
		while(matcher.find()) {
			key = matcher.group(1);
			value = params.get(key);
			matcher.appendReplacement(result, value != null ? String.valueOf(value) : "");
		}
		matcher.appendTail(result);
		return result.toString();
	}
	
	public static String upperCaseFirst(String s) {
		String first = StringUtils.substring(s, 0, 1);
		return StringUtils.join(StringUtils.upperCase(first), StringUtils.substring(s, 1));
	}
	
	public static String lowerCaseFirst(String s) {
		String first = StringUtils.substring(s, 0, 1);
		return StringUtils.join(StringUtils.lowerCase(first), StringUtils.substring(s, 1));
	}
	
	public static String formatStringName(String str) {
		String name = StringUtils.lowerCase(str);
		StringBuilder s = new StringBuilder();
		if(StringUtils.contains(name, "_")) {
			String[] parts = StringUtils.split(name, "_");
			for(String part:parts) {
				s.append(upperCaseFirst(part));
			}
		} else {
			s.append(upperCaseFirst(name));
		}
		return s.toString();
	}
	
}
